<!DOCTYPE html>
<html lang="cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Layui-Tinymce</title>
</head>
<link rel="stylesheet" href="./layui/css/layui.css">

<body>
    <div style="width: 90%;margin: 15px auto;">
        <div class="layui-btn-group" style="margin-top: 10px">
            <button class="layui-btn" lay-event="setByAjax">Ajax加载数据</button>
            <button class="layui-btn" lay-event="setContent">设置内容</button>
            <button class="layui-btn" lay-event="insertContent">插入内容</button>
            <button class="layui-btn" lay-event="getContent">获取内容</button>
            <button class="layui-btn" lay-event="getText">获取文本</button>
            <button class="layui-btn" lay-event="clearContent">清空内容</button>
        </div>
        <hr />
        <textarea name="" id="edit">这里是<span style="color: #e03e2d;"><em>edit初始化</em></span>的内容</textarea>
    </div>

</body>

<script src="./layui/layui.js"></script>
<script>
    /^http(s*):\/\//.test(location.href) || alert('请先部署到 localhost 下再访问');
    layui.extend({
        tinymce: '{/}./layui_exts/tinymce/tinymce'
    }).use(['tinymce', 'util', 'layer'], function () {
        var t = layui.tinymce
            , util = layui.util
            , layer = layui.layer
            , $ = layui.$

        var edit = t.render({
            elem: "#edit"
            , height: 200
            // ...
            // 其余配置可参考官方文档
        }, function (opt, edit) {
            //加载完成后回调
        });

        t.render({
            elem: "#edit2"
            , height: 200
            , init_instance_callback: function (ed) {
                ed.on('KeyPress', function (e) {
                    console.log(e)
                });
            }

        });

        util.event('lay-event', {
            setByAjax: function () {
                $.ajax({
                    url: './layui_exts/mock/content.json',
                    success: function (res) {
                        t.get('#edit').setContent(res.data.content)
                    }
                })
            },
            getContent: function () {
                var content = t.get('#edit').getContent()
                layer.alert(content)
            },
            setContent: function () {
                t.get('#edit').setContent('点击按钮设置的内容：<span style="color: #e03e2d;">' + new Date() + '</span>')
            },
            clearContent: function () {
                t.get('#edit').setContent('')
            },
            insertContent: function () {
                t.get('#edit').insertContent('<b>插入内容</b>')
            },
            getText: function () {
                var text = t.get('#edit').getContent({ format: 'text' })
                layer.alert(text)
            },

        });

    });


</script>

</html>